package com.git.smp.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.MenuMapper;
import com.git.smp.dao.SysOrgMapper;
import com.git.smp.entity.ApplyServerExtend;
import com.git.smp.entity.ApplySystem;
import com.git.smp.entity.Menu;
import com.git.smp.entity.MenuExtend;
import com.git.smp.entity.RequireApply;
import com.git.smp.entity.RequireApplyDetail;
import com.git.smp.entity.SmpUser;
import com.git.smp.entity.SysOrg;
import com.git.smp.entity.SysOrgExtend;
import com.git.smp.entity.SysOrgTree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/org")
public class SysOrgController {

	Logger logger = LoggerFactory.getLogger(SysOrgController.class);

	@Autowired
	MenuMapper menuMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	
	@Autowired
	SysOrgMapper sysOrgMapper;
	
	
	
	 /**
     * 获取机构树状信息
     * @return
     */
	@RequestMapping("/getOrgAllList")
	public ResponseMessage<List<SysOrgExtend>> getOrgAllList() {
		ResponseMessage<List<SysOrgExtend>> responseMessage = new ResponseMessage<List<SysOrgExtend>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");
		
		List<SysOrgExtend> sysOrgList = sysOrgMapper.getOrgNameList();
		
		
	
		
		responseMessage.setExtend(sysOrgList);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
    
	
	
    /**
     * 获取机构树状信息
     * @return
     */
	@RequestMapping("/getOrgNameList")
	public ResponseMessage<List<?>> getOrgNameList() {
		ResponseMessage<List<?>> responseMessage = new ResponseMessage<List<?>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");
		
		List<SysOrgExtend> sysOrgList = sysOrgMapper.getOrgNameList();
		
		SysOrgTree sysOrgTree = new SysOrgTree(sysOrgList);
		
		List<SysOrgExtend> sysOrgList2 = sysOrgTree.buildTree();
		
		Map p1 = new HashMap<>();
		p1.put("seqCate","1");
		p1.put("orgLevel","0");
		p1.put("label","机构信息");
		p1.put("children",sysOrgList2);
		
		List<Map<String,Object>> list2 = new ArrayList<>();
		
		list2.add(p1);
		
		responseMessage.setExtend(list2);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
    
    
 
	
	/**
	 * 根据主键获取机构信息
	 * @param seqCate
	 * @return
	 */
	@RequestMapping("/getOrgById")
	public ResponseMessage<SysOrgExtend> getOrgById(@RequestParam(value = "seqCate", defaultValue = "") String seqCate) {
		ResponseMessage<SysOrgExtend> responseMessage = new ResponseMessage<SysOrgExtend>();
		
		SysOrgExtend syso = sysOrgMapper.getOrgById(seqCate);

		responseMessage.setExtend(syso);
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
	/**
	 * 机构新增
	 * @param sysOrg
	 * @return
	 */
	@RequestMapping("/addOrg")
	public ResponseMessage addOrg(@RequestBody SysOrg sysOrg) {
		ResponseMessage<PageInfo<SysOrg>> responseMessage = new ResponseMessage<PageInfo<SysOrg>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("机构新增成功！");
		
		Date date = new Date();
		sysOrg.setCreateDate(date);
		
		String applyUser = request.getHeader("userName");
		sysOrg.setCreateUser(applyUser);
		
		short a = (short) (sysOrg.getOrgLevel()+1);
		
		sysOrg.setOrgLevel(a);
		
		try {
			sysOrgMapper.insertSelective(sysOrg);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("机构新增失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 机构修改
	 * @param sysOrg
	 * @return
	 */
	@RequestMapping("/updateOrg")
	public ResponseMessage updateOrg(@RequestBody SysOrg sysOrg) {
		ResponseMessage<PageInfo<SysOrg>> responseMessage = new ResponseMessage<PageInfo<SysOrg>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("机构修改成功！");
		
		Date date = new Date();
		sysOrg.setUpdateDate(date);
		
		String applyUser = request.getHeader("userName");
		sysOrg.setUpdateUser(applyUser);
		
		try {
			sysOrgMapper.updateByPrimaryKeySelective(sysOrg);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("机构修改失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 机构删除
	 * @param seqCate
	 * @return
	 */
	@RequestMapping("/deteleOrg")
	public ResponseMessage deteleOrg(@RequestParam(value = "seqCate", defaultValue = "") String seqCate) {
		ResponseMessage<PageInfo<SysOrg>> responseMessage = new ResponseMessage<PageInfo<SysOrg>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("机构删除成功！");
		
		try {
			sysOrgMapper.deleteByPrimaryKey(seqCate);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("机构删除失败！");
		}

		return responseMessage;
	}
	
	
	/**
	 * 
	 * 用户表关联所属部门 
	 * @param seqCate
	 * @return
	 */
	@RequestMapping("/getOrgList")
	public ResponseMessage<List<SysOrg>> getOrgList(
			@RequestBody SysOrg sysOrg
	) {
		ResponseMessage<List<SysOrg>> responseMessage = new ResponseMessage<List<SysOrg>>();
		
		List<SysOrg> orgList = sysOrgMapper.getOrgList(sysOrg);

		responseMessage.setExtend(orgList);
		logger.info(responseMessage.getMessage()); 
		return responseMessage;
	}
	
}
